import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate'

Vue.use(Vuex)


const store = new Vuex.Store({
	state: {
		tdkey: '6ca327c7c1fd5a8760751a7dba9cb8b8',
		request: '', // 带进来的参数
		resultAdd: '',
		System: '',
		startUp: '',
		bid: 'V6EwjIea73YoMPPRe3csEw==', 
		color: '#009874',
		colorVal: '',
		vid: '',  // 100794476
		share_v_id: '',
		userInfo: '',
		logo: '',
		nameDefinition: '',
		share: '',
		App: '', // 小程序信息
		openid: '',
		session_key: '',
		position: { // 经纬度
			longitude: 0,
			latitude: 0,
			// longitude: 106.58273,
			// latitude: 29.544710000000002,
		},
		city: '重庆',
		navWidth: '',
		navHeight: '',
		totalHeight: '',
		cartName: '购物车',
		tabBar: '',
		tabId: 1,
		tabTitle: '首页',
		CityInfo: '', // 选择收货地址 城市选择
		showMode: false, // 首页授权登录弹框
		showShareQr: false, // 首页分享海报
		showKefu: false, // 首页客服
		topic: [],
		regInfo: '',


		showPrompt: false,
		chapterId: '',
		current: 0,
		musicState: false, // true 停止， false 播放,
		times: 1,
		playInfo: {},
		launch: false, // 控制开屏广告只显示一次
		openAds: false, // 首页弹出广告
		audioComp: null, // 首页全局音频实例
		audioConfig: '',
		audioOnPlay: false,
		ordersShop_id: '', // 初始化进入订单页面的门店id
		isContinue: false, // 是否允许下订单
		homeAddress: '',
		homeShopId: ''
	},
	mutations: {
		setHome(state, payload) {
			if (payload.showMode != undefined) state.showMode = payload.showMode;
			if (payload.homeAddress) state.homeAddress = payload.homeAddress
			if (payload.homeShopId) state.homeShopId = payload.homeShopId
		},
		setHomeShare(state, payload) {
			if (payload.showShareQr != undefined) state.showShareQr = payload.showShareQr;
		},
		setHomeKefu(state, payload) {
			if (payload.showKefu != undefined) state.showKefu = payload.showKefu;
		},
		getAddress(state, payload) { // 收货地址
			state.resultAdd = payload
		},
		setSystem(state, payload) {
			state.System = payload;
		},
		getNav(state, payload) {
			state.navWidth = payload.navWidth;
			state.navHeight = payload.navHeight;
			state.totalHeight = payload.totalHeight;
		},
		setApplet(state, payload) {
			state.App = payload;
		},
		getUser(state, payload) {
			if (payload.bid) state.bid = payload.bid;
			if (payload.vid || payload.vid == '') state.vid = payload.vid;
			if (payload.openid) state.openid = payload.openid;
			if (payload.session_key) state.session_key = payload.session_key;
			if (payload.share_v_id) state.share_v_id = payload.share_v_id;
			if (payload.share) state.share = payload.share;
			if (payload.logo) state.logo = payload.logo;
			if (payload.nameDefinition) state.nameDefinition = payload.nameDefinition;
			if (payload.userInfo) state.userInfo = payload.userInfo;
			if (payload.tabBar) state.tabBar = payload.tabBar;
			if (payload.startUp) state.startUp = payload.startUp;
			if (payload.color) state.color = payload.color;
			if (payload.colorVal) state.colorVal = payload.colorVal;
			if (payload.request) state.request = payload.request;
			if (payload.regInfo) state.regInfo = payload.regInfo;

		},
		//  获取经纬度
		Position(state, payload) {
			state.position = payload;
		},
		changeTab(state, payload) {
			state.tabId = payload.tabId;
			state.tabTitle = payload.tabTitle;
		},
		SeletCity(state, payload) {
			state.CityInfo = payload;
			state.city = payload.name || payload.fullname;
		},
		changeCity(state, payload) {
			state.city = payload;
		},
		getTopic(state, payload) { // 帖子话题分类
			state.topic = payload;
		},

		setChapter_id(state, payload) {
			state.chapterId = payload;
		},
		setPlayInfo(state, payload) {
			if (payload.playInfo) state.playInfo = payload.playInfo;
			if (payload.times) state.times = payload.times;
		},
		setState(state, payload) {
			state.musicState = payload;
		},
		setCurrent(state, payload) {
			state.current = payload
		},
		setLaunch(state, payload) {
			if (payload.launch != undefined) state.launch = payload.launch;
			if (payload.openAds != undefined) state.openAds = payload.openAds;
		},
		setPrompt(state, payload) {
			state.showPrompt = payload
		},
		setAudio(state, payload) {
			if (payload && payload.audioOnPlay != undefined) { // 改变状态
				state.audioOnPlay = payload.audioOnPlay
				state.audioOnPlay ? state.audioComp.play() : state.audioComp.pause()
				return
			}
			if (payload) { // 音频组件配置
				state.audioConfig = payload
				if (state.audioComp || !payload.style || !payload.global) return
				state.audioOnPlay = payload.autoplay
				let audio = uni.createInnerAudioContext()
				audio.src = payload.audioSrc
				audio.autoplay = payload.autoplay
				audio.loop = payload.loop
				state.audioComp = audio
			} else { // null-仅销毁音频、不传直接清空配置
				if (payload === undefined) state.audioConfig = ''
				if (!state.audioComp) return
				state.audioComp.destroy()
				state.audioComp = null
			}
		},
		updateShopId(state, payload) {
			state.ordersShop_id = payload
		},
		updateOrderstatus(state, payload) {
			state.isContinue = payload;
		}
	},
	getters: {
		themeColor: state => `--color:${state.color}`
	},


	plugins: [
		createPersistedState({
			key: 'twofourbanquet',
			paths: [
				'tdkey',
				'request',
				'resultAdd',
				'System',
				'startUp',
				'bid',
				'vid',
				'color',
				'colorVal',
				'share_v_id',
				'logo',
				'nameDefinition',
				'userInfo',
				'share',
				'App',
				'openid',
				'session_key',
				'position',
				'city',
				'navWidth',
				'navHeight',
				'totalHeight',
				'cartName',
				'tabBar',
				'tabId',
				'tabTitle',
				'CityInfo',
				'showMode',
				'regInfo',
				'topic',
				'chapterId',
				'current',
				'times',
				'playInfo',
				'musicState',
				"launch",
				"showPrompt",
				"openAds",
				"ordersShop_id",
				"isContinue"
			],
			storage: {
				getItem: (key) => uni.getStorageSync(key), // 获取
				setItem: (key, value) => uni.setStorageSync(key, value), // 存储
				removeItem: (key) => uni.removeStorageSync(key) // 删除
			}
		})

	]
})

export default store